package Service.Implement;

import Dao.Implement.adminDaoImplement;
import Dao.Implement.roomDaoImplement;
import Dao.Implement.roomUserDaoImplement;
import Dao.Implement.userDaoImplement;
import Dao.adminDao;
import Dao.roomDao;
import Dao.roomUserDao;
import Dao.userDao;
import Service.systemService;
import org.springframework.stereotype.Service;
import pojo.*;

import java.util.*;

@Service(value = "systemServiceImplement")
public class systemServiceImplement  implements systemService {
    adminDao adminDao=new adminDaoImplement();
    roomUserDao roomUserDao=new roomUserDaoImplement();
    roomDao roomDao=new roomDaoImplement();
    userDao userDao=new userDaoImplement();

    private String lastupdatePKTime="2021-07-21";//记录上次调用更新PK值的时间
    private  int totalSports=8;
    double factor=0.85;//PageRank调整系数

    public  void updateBitsMap() {
                    List<stadium> stadiumList=adminDao.queryStadiumWithNoArguments();
                    for( stadium instance :stadiumList){
                        int opentime=instance.getOpentime();
                        int closetime=instance.getClosetime();
                        Long id=instance.getStadium_id();
                        List<playground> playgroundList=adminDao.queryPlaygroundListByStadium(id);
                        for(playground playground:playgroundList){
                            //天数的更新
                            playground.setBitmaps_today(playground.getBitmaps_tomorrow());
                            playground.setRealTime(opentime,closetime,"tomorrow");
                            adminDao.updatePlaygroundState(playground);
                        }
            }

        }

    @Override
    public void updateUserPk(String date) {

        //查询room_user中的两次调用之间的场数来更新PK
       List<room_user> recordList=roomUserDao.selectUserRecord(lastupdatePKTime,date);

       int TotalRecordNum=recordList.size();

        System.out.println(recordList);
        // 0表示未定 1表示用户赢 2表示用户失败

        //统计处理过的用户id
        ArrayList<Long> handledUser=new ArrayList<Long>();

        for( room_user instance: recordList){
            //得到当前用户的id
            Long id=instance.getUser_id();
            //得到当前用户的实例
            user user= userDao.queryUserByID(id);
            //用一个1*8的数组统计各项运动输的场数
            int[] loseMatrix=new int[totalSports+1];

            //判断该元素是否被处理过
            if(!handledUser.contains(id)){

            for(int i=1;i<=totalSports;i++){
                loseMatrix[i]=Integer.parseInt(roomUserDao.queryLoseByCategory(id,new Integer(i).toString(),lastupdatePKTime,date,2).toString());
            }


            //对当前用户loseMatrix进行处理
            for(int i=1;i<=totalSports;i++){
                double currentUserPK=userDao.queryUserByID(id).getPKBYCategory(i);
                //如果有胜负场对其进行处理
                if(loseMatrix[i]!=0){
                    //找出当前种类用户输掉的房间列表
                    ArrayList<Long> roomList=new ArrayList<Long>();
                    for(room_user bianli:recordList){
                        if(bianli.getUser_id().equals(id)&&bianli.getUser_state()==2&&Integer.parseInt(bianli.getCategory())==i){
                            roomList.add(bianli.getRoom_id());
                        }
                    }
                    //根据以上得到的房间列表找出赢得玩家并将其对应类别的PK值进行更新
                    for(Long roomID:roomList){
                        //得到其它用户
                        List<user> userList=userDao.queryUserIDByRoomID(roomID);
                        for(user user1:userList){
                            //根据类别更新运动实力值
                          double PK=currentUserPK/loseMatrix[i]*factor+(1-factor)/TotalRecordNum;
                          //写回更新
                          user1.setPKBYCategory(i,PK);
                          userDao.updateUserPK(user1,user1.getStudent_id());
                        }
                    }
                }
            }
            }
            handledUser.add(id);

        }
       lastupdatePKTime=date;

    }
}